#include "imagesort.h"

ImageSort::ImageSort()
{}

ImageSort::ImageSort(const QStringList& list) :
    theList(list)
{
    int size = theList.size();
    if(size > 0)
    {
        temp[size];
        merge_sort(0, theList.size()-1);
    }
}

ImageSort::~ImageSort()
{
 }

void ImageSort::merge_sort(int low,int high)
{
    int mid;
    if(low<high)
    {
        mid=(low+high)/2;
        merge_sort(low,mid);
        merge_sort(mid+1,high);
        merge(low,mid,high);
    }
}
void ImageSort::merge(int low,int mid,int high)
{
    int h,i,j,k;
    h=low;
    i=low;
    j=mid+1;

    while((h<=mid)&&(j<=high))
    {
        QFileInfo f1 = QFileInfo(theList.at(h));
        QFileInfo f2 = QFileInfo(theList.at(j));

        if(f1.lastModified() <= f2.lastModified())
        {
            temp[i] = theList.at(h);
            h++;
        }
        else
        {
            temp[i] = theList.at(j);
            j++;
        }
        i++;
    }
    if(h>mid)
    {
        for(k=j;k<=high;k++)
        {
            temp[i] = theList.at(k);
            i++;
        }
    }
    else
    {
        for(k=h;k<=mid;k++)
        {
            temp[i] = theList.at(k);
            i++;
        }
    }
    for(k=low;k<=high;k++)
        theList.replace(k, temp[k]);
}
